#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "string.h"
/* Determine whether arguments can be added without overflow. */
int tadd_ok(int x, int y)
{
    //if there isn't any overflow return 1
    //else return 0
    int sum = x + y;

    if(y == -y)
    {
            if(x >= 0 )
            {
                printf("%s   there is an overflow\r\n",__func__);
                printf("sum is %x\r\n",sum);
                 return 0;
            }
            else
            {
                printf("%s   there isn't any overflow\r\n",__func__);
                printf("sum is %x\r\n",sum);
                return 1;

            }


    }

    else {

        if(((x < 0) && (y < 0) && (sum >= 0)) || ((x > 0) && (y > 0) && (sum < 0)))
        {
             printf("%s   there is an overflow\r\n",__func__);
             printf("sum is %x\r\n",sum);
             return 0;
        }
        else
        {
             printf("%s   there isn't any overflow\r\n",__func__);
             printf("sum is %x\r\n",sum);
             return 1;

        }

    }
}
/* Bug warning*/
int tsub_ok(int x, int y)
{
    // When y == Tmin and x < 0. There will be a bug



    return tadd_ok(x,-y);


}
 int String_To_Int(char *string, int Length)
{

    int  Temp_Value = 0;
    int i = 0;
    int Times_16 = 1;

    //Caculate the length of the string
    printf("The Length is %d\r\n",Length);

    //Convert every character to a value
    for( i = Length - 1 ; i >= 0;i--)
    {
       if((string[i] >= 0x30) && (string[i] <= 0x39))
       {
           Temp_Value += ((string[i] - 0x30)*Times_16);
           Times_16 *= 16;
         } else if((string[i] >= 0x41) &&( string[i] <= 0x46)) { Temp_Value += ((string[i] - (0x41 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else if((string[i] >= 0x61) &&( string[i] <= 0x66))
       {
           Temp_Value += ((string[i] - (0x61 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else
       {
           printf("Wrong number!\r\n");
           return -1;
       }
       printf(" string[i] is %c and Temp_Value is %x \r\n",string[i],Temp_Value);
    }
    printf("Temp_Value is %x\r\n",Temp_Value);

    return Temp_Value;
}

int bis(int x, int m)
{
    //令 a = m - ( 1 << i) 然后将a 的值右移i位的到的值与 m右移i位的值进行比较
    // 如果这两个值相等，说明在减1的时候没有发生借位，则m的bit[i]为1，此时再以同样的方式比较x,如果x的值前后不相等,说明x的bit[i]为0
    // 那么就需要把x的bit[i] 置位，也就是+1

    int temp,i,j = 0;
    i = j;


    int a,b = 0;
    a = 0;
    //这里有3种情况
    //x的有效位数比m的有效位数大
    //           等于
    //           小于
    //下面的2个while循环用来计算各自的有效位数
    //Modification begin 2015.12.5#################################################################################
    //x is the source , m is the mask

    temp = 1;
    while( m > temp)
    {
       temp = temp << 1;
       b++;
    }
    temp = 1;
    while( x > temp)
    {
        temp = temp << 1;
        a++;
    }

    printf("x is %x  m is %x\r\n",x,m);
    printf("    %s line %d a is %d b is %d\r\n",__func__,__LINE__,a,b);
    if( a >= b)
    {
        printf("   %s line %d b is %x\r\n",__func__,__LINE__,b);
        {
            for(i = 0;i < b;i++)
            //    x m 0 1
            //    0   * 1
            //    1   * *
             if( ((m - (1 << i)) >> (i + 1)) == (m >> (i+1)))
            {

                if( ((x - (1 << i)) >> (i + 1)) != (x >> (i+1)))
                {
                     x += (1 << i);
                    printf("x is %x m is %x\r\n",x,m);
                 }

             }
        }
        printf("    %s line %d return value is %x\r\n",__func__,__LINE__,x);
        return x;
    }
    else
    {
        printf("   %s line %d a is %x\r\n",__func__,__LINE__,a);
        {
            for(i  = 0; i  < a; i++)
            //    x m 0 1
            //    0   * 1
            //    1   * *
             if( ((x - (1 << i)) >> (i + 1)) == (x >> (i+1)))
            {

                if( ((m - (1 << i)) >> (i + 1)) != (m >> (i+1)))
                {
                     m += (1 << i);
                    printf("x is %x m is %x\r\n",x,m);
                 }

             }
        }
        printf("    %s line %d return value is %x\r\n",__func__,__LINE__,m);
        return m;
    }
//Modification  end  2015.12.5#################################################################################

}

int bic(int x, int m)
{
    //Modification begin    2015.12.5
    // &~ complements

    int temp,i,j = 0;
    i = j;


    int a,b = 0;
    a = 0;



    //这里有3种情况
    //x的有效位数比m的有效位数大
    //           等于
    //           小于
    //x is the source , m is the mask

    temp = 1;
    while( m > temp)
    {
       temp = temp << 1;
       b++;
    }
    temp = 1;
    while( x > temp)
    {
        temp = temp << 1;
        a++;
    }

    printf("    %s line %d b is %x\r\n",__func__,__LINE__,b);
    for(i = 0; i < ((a < b)?a:b) ; i++)
    {
        //
        //    x m 0 1
        //    0   * 0
        //    1   * 0
        if( ((m - (1 << i)) >> (i + 1)) == (m >> (i+1)))
        {

            if( ((x - (1 << i)) >> (i + 1)) == (x >> (i+1)))
            {
                printf("%s line%d m is %x x is %x\r\n",__func__,__LINE__,m,x);
                x -= (1 << i);
            }

        }
    }
    printf("    %s line %d return value is %x\r\n",__func__,__LINE__,x);
    return x;
    //Modification end    2015.12.5
}
int bool_or(int x, int y)
{
    //Modification begin 2015.12.5
    //    x y 0 1
    //    0   0 1
    //    1   1 1
    int result = bis(x,y);

    return result;
    //Modification end  2015.12.5
}
int bool_xor(int x, int y)
{
    //Modification begin 2015.12.5
    //    XOR               bis           bic
    //    x y 0 1           x y 0 1       x y 0 1
    //    0   0 1           0   0 1       0   0 0
    //    1   1 0           1   1 1       1   1 0

    int result = bis(bic(x,y),bic(y,x));
    return result;
    //Modification end 2015.12.5
}
int IsEqual(int x, int y)
{
    return !(x ^y);



}
int main(int argc, char *argv[])
{
    int *x = 0;
    int Temp_Value[2] = {0,};
    int value = 0;
    int Length = 0;
    int resault;

    Temp_Value[0] = String_To_Int(argv[1],strlen(argv[1]));
    Temp_Value[1] = String_To_Int(argv[2],strlen(argv[2]));
    resault = tsub_ok(Temp_Value[0],Temp_Value[1]);
    printf("The resault is %d\r\n",resault);
    if(resault)
        printf("%s   There isn't any overflow\r\n",__func__);
    else
        printf("%s   There is an overflow\r\n",__func__);






    return 0;
}
